package yxy.game.pm2.command.plainsCraft;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsCraftMessage;
import yxy.game.pm2.module.plainsCraft.PlainsCraftPlayerModule;

/** 中原争霸 购买挑战次数 */
@MessageType(proto = Command.PlainsCraftFormatTimesDeduct, classtype = PlainsCraftMessage.PlainsCraftFormatTimesDeductRequest.class)
public class CMD_PlainsCraftFormatTimesDeduct extends Command<PlainsCraftMessage.PlainsCraftFormatTimesDeductRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsCraftMessage.PlainsCraftFormatTimesDeductRequest request) {
		String playerCode = messageSource.getTag();

		PlainsCraftMessage.PlainsCraftFormatTimesDeductResponse.Builder response = PlainsCraftMessage.PlainsCraftFormatTimesDeductResponse.newBuilder();

		try {
			PlainsCraftPlayerModule module = PlainsCraftPlayerModule.get(playerCode);
			if (module == null) {
				send(messageSource, response, Result.ModuleNotOpen);// 模块没有开启
				return;
			}

			module.setUsedFormatTimes(1);
			module.set();

			response.setIsFormatInit(module.getIsFormatInit());
			response.setFormatTimes(PlainsCraftPlayerModule.getFormatTimes(module));
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void send(MessageSource messageSource, PlainsCraftMessage.PlainsCraftFormatTimesDeductResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}